/*
 * @Descripttion: 
 * @version: 
 * @Author: ada.wu
 * @Date: 2024-01-05 10:17:42
 * @LastEditors: ada.wu
 * @LastEditTime: 2024-04-09 20:39:11
 */
import { Module } from 'vuex';
// 此处加上 `.ts` 后缀报错，具体原因不详
import { UsersState, RootStateTypes } from '../interface';
import storageUtil from "../../util/storageUtil";
// import { getUserInfo } from "/@/api/UserApi";
import { getUserInfoApp } from "/@/api/ScrmApi";
const usersModule: Module<UsersState, RootStateTypes> = {
    namespaced: true,
    state: {
        //钉钉授权用户信息
        userDingDTO: {
            associatedUnionid: "",// null,
            deviceId: "",// "34aac6d5534bf8a745aeafa7487e6079",
            name: "",// "程乔",
            sys: "",// false,
            sysLevel: "",// 0,
            unionid: "",// "kGqGW8FFf7ciShZ1Wwwbz7AiEiE",
            userid: "",// "1326093227988585"
        },

    },
    mutations: {
        // 设置钉钉用户信息
        setUserDTO(state: any, data: any) {
            console.error('setUserDTO---', data)
            state.userDingDTO = data;
            storageUtil.setDingUserInfo(data)
        },
        // 清空用户信息
        removeUserDTO(state: any, data: object) {
            state.userDingDTO = {}
            state.familyInfo = {}
        },
        loginOut(state: any, data: any) {
            state.userDingDTO = {}
            storageUtil.removeSystemCode()
            storageUtil.removeToken()
            storageUtil.removeCustomerAuthorization()
        },
    },
    actions: {
        // 设置用户信息
        async setUserInfo({ commit }) {
            return new Promise<any>((resolve, reject) => {
                let userDTO = storageUtil.getDingUserInfo()
                console.log('USER_DTO===',userDTO)
                if (userDTO) {
                    commit('setUserDTO', userDTO);
                    resolve(userDTO)
                } else {
                    reject()
                }

                // getUserInfoApp(data).then(userDTO => {
                //     commit('setUserDTO', userDTO);
                //     resolve(userDTO)
                // }).catch(() => {
                //     commit("removeUserDTO")
                //     storageUtil.removeToken()
                //     storageUtil.removeCustomerAuthorization()
                //     storageUtil.removeDingUserInfo()
                //     // storageUtil.removeSystemCode()
                //     reject()
                // })
            })
        },
        // 设置用户信息
        async getUserInfo({ commit }, data) {
            console.error('data==', data)
            return new Promise<any>((resolve, reject) => {
               
                getUserInfoApp(data).then(userDTO => {
                    commit('setUserDTO', userDTO);
                    resolve(userDTO)
                }).catch(() => {
                    commit("removeUserDTO")
                    storageUtil.removeToken()
                    storageUtil.removeCustomerAuthorization()
                    storageUtil.removeDingUserInfo()
                    // storageUtil.removeSystemCode()
                    reject()
                })
            })
        },
        async getLoginByDebug({ commit }, data) {
            console.error('data==', data)
            return new Promise<any>((resolve, reject) => {
                let userDTO =
                {
                    "associatedUnionid": null,
                    "deviceId": "34aac6d5534bf8a745aeafa7487e6079",
                    "name": "程乔",
                    "sys": false,
                    "sysLevel": 0,
                    "unionid": "kGqGW8FFf7ciShZ1Wwwbz7AiEiE",
                    "userid":data.userid|| "1326093227988585"
                }
                commit('setUserDTO', userDTO);
                resolve(userDTO)

             
            })
        },
    },
};

export default usersModule;
